<?php  if ( ! defined('DIR_ROOT')) exit('No direct script access allowed');
/**
 * 全局重要的常用函数
 * @param $file
 * @return unknown_type
 */


/**
 * 是否真的可读
 * @param $file
 * @return unknown_type
 */
function is_really_writable($file)
{	
	if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") === FALSE)
	{
		return is_writable($file);
	}

	if (is_dir($file))
	{
		$file = rtrim($file, '/').'/'.md5(rand(1,100));

		if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
		{
			return FALSE;
		}

		fclose($fp);
		@chmod($file, DIR_WRITE_MODE);
		@unlink($file);
		return TRUE;
	}
	elseif (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
	{
		return FALSE;
	}

	fclose($fp);
	return TRUE;
}

/**
 * 单例加载类
 * @param $class
 * @param $instantiate
 * @return unknown_type
 */
function &load_class($class, $instantiate = TRUE , $param = array(0))
{
    global $ck_config;//全局前缀
	static $objects = array();
	
    $ck_prefix = isset($ck_config['class_prefix'])? $ck_config['class_prefix']: 'CK_';
	if (isset($objects[$class]))
	{
		return $objects[$class];
	}

	$is_ck_class = FALSE;
	$name = $class;
	//添加CK前缀判断
	if (file_exists(DIR_CLASS . '/' . $ck_prefix . $class.EXT))
	{
		require(DIR_CLASS . '/' . $ck_prefix . $class.EXT);
		$is_ck_class = TRUE;
		$name = $ck_prefix . $class;
	}
	else
	{
		require(DIR_LIBRARY . '/' .$class.EXT);
		$is_ck_class = FALSE;
	}

	//不实例化
	if ($instantiate === FALSE)
	{
		$objects[$class] = TRUE;
		return $objects[$class];
	}

	if($param != null)
	{
	   	$objects[$class] =& instantiate_class(new $name($param)); 
	}
	else
	{
	    $objects[$class] =& instantiate_class(new $name());
	}
	return $objects[$class];
}

/**
 * @param $class_object
 * @return unknown_type
 */
function &instantiate_class(&$class_object)
{
	return $class_object;
}


